<?php

namespace common\models\position;

use Yii;

/**
 * This is the model class for table "{{%position}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $thumb 位置图示
 * @property int $width 宽度
 * @property int $height 高度
 * @property int $limits 内容限制数目
 * @property int $status 状态：0禁用，10启用
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 * @property int $deleted_at 删除时间
 */
class Position extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    const CACHE_DURATION = 60;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%position}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['width', 'height', 'limits', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 64],
            [['thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'thumb' => '位置图示',
            'width' => '宽度',
            'height' => '高度',
            'limits' => '内容限制数目',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getData() {
        return $this->hasMany(PositionData::className(), ['position_id' => 'id'])->orderBy([
            'updated_at' => SORT_DESC
        ]);
    }

    public function getList() {
        $query = $this->getData()
        ->andWhere(['status' => PositionData::STATUS_ACTIVE])
        ->limit($this->limits)
        ->orderBy([
            'listorder' => SORT_ASC,
            'updated_at' => SORT_DESC
        ]);
        if (YII_DEBUG) {
            $query->cache(60);
        }
        return $query;
    }

}
